<template>
  <div>
    <div class="specifications">
      <a class="titleBox" href="//list.vip.com/brand.html?sn=10004065">{{
        goodsInfo.skuName
      }}</a>
      <p class="comTitle">
        <span class="vipZy">唯品自营</span>
        2{{ goodsInfo.skuName }}
      </p>
      <div class="jinE">
        <div class="jinELeft">
          <div class="xianJia">
            <b>¥</b>
            <span>{{ goodsInfo.price }}</span>
          </div>
          <p>
            <span>¥{{ goodsInfo.price }}</span>
            3.6折 | 近60天内已降¥25
          </p>
        </div>
        <span class="jinERight"
          >5&nbsp; 天 &nbsp;00&nbsp; 小时 &nbsp; 后结束</span
        >
      </div>
      <div class="attrBox">
        <form action="">
          <table>
            <tr>
              <td>配送</td>
              <td class="peiSong">
                <p type="text" name="" id="">
                  <span>xxx xxx xxx</span>
                  <span class="iconfont icon-jiantouxia"></span>
                </p>
              </td>
            </tr>
            <tr>
              <td>运费</td>
              <td class="baoYou">
                新会员专享首单满38元免邮（限唯品自营商品，部分商品不可用）
              </td>
            </tr>
            <tr v-for="AttrValue in attrValueList" :key="AttrValue.attrId">
              <td>{{ AttrValue.attrName }}</td>
              <td class="saleTb">
                <a class="imgColor" href="javascript:;">
                  <!-- <img
                    src="//h2.appsimg.com/a.appsimg.com/upload/merchandise/pdcvis/2022/07/15/31/70f8126b-bf75-4b47-b8fe-f89966ee3b11_420x420_90.jpg"
                    alt=""
                  /> -->
                  <span>yyy</span>
                </a>
              </td>
            </tr>

            <tr>
              <td>数量</td>
              <td>
                <div class="coxJJ">
                  <button class="jia" @click.prevent="subHandel">-</button>
                  <input
                    autocomplete="off"
                    class="num"
                    v-model.number="num"
                    @change="loseBlur"
                  />
                  <button class="jian" @click.prevent="num++">+</button>
                </div>
              </td>
            </tr>
            <tr>
              <td></td>
              <td class="buttonTow">
                <button class="btnOne">
                  <span @click="addShopToCart">加入购物车</span>
                </button>
                <button class="btnTow">
                  <span>¥{{ goodsInfo.price }}</span>
                </button>
              </td>
            </tr>
          </table>
        </form>
      </div>
    </div>
    <div class="fuWu">
      <table class="tableFw">
        <tr>
          <td></td>
          <td class="shouJi">
            <span class="iconfont icon-shouji"></span>
            在小程序中查看此商品
          </td>
        </tr>
        <tr>
          <td>客服</td>
          <td>
            <span class="iconfont icon-xiaoxizhongxin"></span>
            <a href="">在线客服</a>
            (09:00-23:00)
          </td>
        </tr>
        <tr>
          <td>服务</td>
          <td>
            <span class="iconfont icon-wancheng"></span>
            首单38元包邮
            <span class="iconfont icon-wancheng"></span>
            7天无理由
            <span class="iconfont icon-wancheng"></span>
            10天保价
            <span class="iconfont icon-wancheng"></span>
            上门退货
          </td>
        </tr>
      </table>
    </div>
  </div>
</template>

<script>
import { reqAddData, requpdateData } from "@/api/detail";
import { mapState } from "vuex";
export default {
  name: "Specifications",
  data() {
    return {
      num: 1,
    };
  },
  props: ["goodsInfo"],
  mounted() {
    this.$store.dispatch("getShopcartList");
  },
  computed: {
    attrValueList() {
      // 解决假报错
      return this.goodsInfo.skuAttrValueList?.slice(0, 2) || [];
    },
    imgList() {
      // 解决假报错
      return this.goodsInfo.skuImageList || [];
    },
    ...mapState({
      shopcartList: (state) => state.shopcart.shopcartList,
    }),
  },
  methods: {
    // 商品数量递减
    subHandel() {
      if (this.num === 1) {
        this.num = 1;
      } else {
        this.num--;
      }
    },
    // 数量输入框失去焦点时 修改数量
    loseBlur() {
      if (this.num < 1) {
        this.num = 1;
      }
    },
    // 添加购物车
    async addShopToCart() {
      const obj = {
        title: this.goodsInfo.skuName,
        imgUrl: this.goodsInfo.skuDefaultImg,
        productId: Math.floor(Math.random() * 10000000000000000 - 1),
        name: this.goodsInfo.skuDesc,
        id: this.goodsInfo.id,
        sizeName: this.goodsInfo.skuAttrValueList[0].valueName,
        priceExcludePms: this.goodsInfo.price,
        payAll: "未完成",
        num: this.num,
        payTime: this.goodsInfo.createTime,
        isChecked: false,
      };
      // 查找对应id的数据，如果有就是添加的是同一数据，我们只需要更改他的num值，
      // 发送修改数据的请求，如果美哟找到就是undefind，则说明数据添加的数据是新数据，
      // 我们只需要发生添加数据的请求
      const result = this.shopcartList.find((item) => {
        return item.id === obj.id;
      });
      if (result) {
        //添加的是同一数据
        obj.num = obj.num + result.num;
        await requpdateData(obj.id, obj);
        this.$message.success("修改添加购物车成功");
      } else {
        //添加的数据是新数据
        await reqAddData(obj);
        this.$message.success("添加购物车成功");
      }
      // 跳转到购物车页面
      this.$router.push("/addcartsuccess");
    },
  },
};
</script>

<style lang="less" scoped>
.specifications {
  width: 550px;
  height: 550px;
  border-bottom: 1px dashed #ccc;
  text-align: left;
  .titleBox {
    display: block;
    margin-top: 10px;
  }
  a {
    font-size: 16px;
    color: #007cff;
    text-decoration: none;
    // margin-top: 5px;
  }
  .comTitle {
    font-size: 16px;
    font-weight: 700;
    margin: 8px 0;
    .vipZy {
      display: inline-block;
      width: 50px;
      height: 18px;
      text-align: center;
      line-height: 18px;
      font-size: 12px;
      font-weight: 400;
      color: #fff;
      background-color: #f53b6a;
    }
  }
  .jinE {
    width: 100%;
    height: 94px;
    overflow: hidden;
    // border: 1px solid black;
    background-image: url("//h2.appsimg.com/b.appsimg.com/upload/momin/2022/06/15/20/1655260223707.png");
    background-size: 100%;
    display: flex;
    justify-content: space-between;
    .jinELeft {
      margin-left: 20px;
      padding-top: 15px;
      .xianJia {
        b {
          font-size: 24px;
          color: #fff;
        }
        span {
          font-size: 38px;
          font-weight: 700;
          color: #ffffff;
        }
      }
      p {
        margin-top: 5px;
        font-size: 16px;
        color: #fff;
        span {
          text-decoration: line-through;
        }
      }
    }
    .jinERight {
      display: inline-block;
      height: 94px;
      line-height: 94px;
      color: #fff;
      font-size: 12px;
      margin-right: 15px;
    }
  }
  .attrBox {
    // border: 1px solid black;
    margin-top: 20px;
    color: #999;
    form {
      table {
        // border: 1px solid #b8b8b8;
        tr td:first-of-type {
          width: 40px;
        }
        tr {
          td {
            height: 50px;
          }
          .peiSong {
            p {
              box-sizing: border-box;
              font-size: 14px;
              margin: 0;
              padding: 6px 15px;
              width: 300px;
              height: 30px;
              border: 1px solid #ccc;
              border-radius: 5px;
              position: relative;
              .iconfont {
                position: absolute;
                right: 8px;
              }
            }
          }
          .saleTb {
            display: flex;
          }
          .baoYou {
            font-size: 14px;
            color: black;
          }
          .imgColor {
            display: flex;
            align-items: center;
            border: 1px solid #ccc;
            width: 80px;
            height: 30px;
            justify-content: center;
            margin: 5px 8px 5px 0;

            img {
              width: 30px;
              margin-right: 10px;
            }
            span {
              color: black;
              font-size: 13px;
            }
          }
          .imgColor:focus {
            border: 2px solid #f53b6a;
          }
          // .imgColorIs {
          //   border: 2px solid #f53b6a;
          // }
          ul,
          li {
            padding: 0;
            margin: 0;
            list-style: none;
          }
          ul {
            display: flex;
            flex-wrap: wrap;
            // width: 300px;
            li {
              width: 80px;
              height: 32px;
              border: 1px solid #ccc;
              text-align: center;
              padding: 8px;
              box-sizing: border-box;
              margin: 5px 10px 5px 0px;
              cursor: pointer;
            }
            #liFocus {
              border: 2px solid #f53b6a;
            }
          }
          .coxJJ {
            width: 80px;
            height: 32px;
            border: 1px solid #ccc;
            position: relative;
            button {
              border: none;
              height: 100%;
              width: 20px;
              position: absolute;
            }
            .jia {
              top: 0;
              cursor: pointer;
              left: 0;
              border-right: 1px solid #ccc;
            }
            .jian {
              cursor: pointer;
              top: 0;
              right: 0;
              border-left: 1px solid #ccc;
            }
            .num {
              width: 100%;
              height: 32px;
              box-sizing: border-box;
              border: 1px solid #ccc;
              overflow: hidden;
              margin: 0;
              text-align: center;
              padding: 10px 0;
              outline: none;
            }
          }
          .buttonTow {
            button {
              height: 45px;
              width: 125px;
              border: none;
              cursor: pointer;
              span {
                color: #fff;
                font-size: 20px;
              }
            }
            .btnOne {
              background-color: #f688a4;
              span {
                // text-decoration: line-through;
              }
            }
            .btnTow {
              background-color: #f03867;
            }
          }
        }
      }
    }
  }
}
.fuWu {
  .tableFw {
    text-align: left;
    font-size: 12px;
    color: #999;
    tr {
      .shouJi {
        color: #f53b6a;
      }
      td {
        padding: 5px;
        .iconfont {
          color: #f53b6a;
          font-weight: 900;
        }
        a {
          color: #f53b6a;
        }
      }
    }
  }
}
</style>
